package main

import (
	"errors"
	"fmt"
)

type commonError struct {
	errorCode int
	errorMsg  string
}

func (c *commonError) Error() string {
	return c.errorMsg
}
func main() {
	defer func() {
		if p := recover(); p != nil {
			fmt.Println(p)
		}
	}()
	result, err := add(-1, -2)
	if err != nil {
		if er, ok := err.(*commonError); ok {
			fmt.Println(er.errorCode, er.errorMsg)
		}
	}
	fmt.Println(result)
	e := errors.New("原始错误")
	w := fmt.Errorf("新的错误，%w", e)
	fmt.Println(w)
	fmt.Println(errors.Unwrap(w))
	fmt.Println(errors.Is(w, e))
	//fmt.Println(errors.As(e, &commonError{}))

	fmt.Println("test")
}

func add(a, b int) (int, error) {
	if a < 0 || b < 0 {
		panic("error")
		return 0, &commonError{errorCode: 1, errorMsg: "参数必须大于0"}
	}
	return a + b, nil
}
